package com.zhaoyang.servlet;

import com.zhaoyang.dao.StudentDao;
import com.zhaoyang.model.Page;
import com.zhaoyang.model.Student;
import com.zhaoyang.util.SnGenerateUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 赵扬
 * @time 2020/4/28 12:56
 */
@WebServlet("/studentServlet")
public class StudentServlet extends HttpServlet {

    private static final long serialVersionUID = -2303373467552793263L;

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String method = request.getParameter("method");
        if ("toStudentListView".equals(method)) {
            studentList(request, response);
        } else if ("AddStudent".equals(method)) {
            addStudent(request, response);
        } else if ("StudentList".equals(method)) {
            getStudentList(request, response);
        } else if ("EditStudent".equals(method)) {
            editStudent(request, response);
        } else if ("DeleteStudent".equals(method)) {
            deleteStudent(request, response);
        }
    }

    private void deleteStudent(HttpServletRequest request, HttpServletResponse response) {

        String[] ids = request.getParameterValues("ids[]");

        StudentDao studentDao = new StudentDao();
        if (studentDao.deleteStudent(ids)) {
            try {
                response.getWriter().write("success");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 修改学生资料
     *
     * @param request
     * @param response
     */
    private void editStudent(HttpServletRequest request, HttpServletResponse response) {

        String name = request.getParameter("name");
        int id = Integer.parseInt(request.getParameter("id"));
        String sex = request.getParameter("sex");
        String mobile = request.getParameter("mobile");
        String qq = request.getParameter("qq");
        int clazzId = Integer.parseInt(request.getParameter("clazzid"));
        Student student = new Student();
        student.setClazzId(clazzId);
        student.setMobile(mobile);
        student.setName(name);
        student.setId(id);
        student.setQQ(qq);
        student.setSex(sex);
        StudentDao studentDao = new StudentDao();
        if (studentDao.editStudent(student)) {
            try {
                response.getWriter().write("success");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void getStudentList(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("studentName");
        Integer currentPage = request.getParameter("page") == null ? 1 : Integer.parseInt(request.getParameter("page"));
        Integer pageSize = request.getParameter("rows") == null ? 999 : Integer.parseInt(request.getParameter("rows"));
        Integer clazz = request.getParameter("clazzid") == null ? 0 : Integer.parseInt(request.getParameter("clazzid"));
        //获取当前登录用户类型
        int userType = Integer.parseInt(request.getSession().getAttribute("userType").toString());
        Student student = new Student();
        student.setName(name);
        student.setClazzId(clazz);

        if (userType == 2) {
            //如果是学生，只能查看自己的信息
            Student currentUser = (Student) request.getSession().getAttribute("user");
            student.setId(currentUser.getId());
        }
        StudentDao studentDao = new StudentDao();
        List<Student> clazzList = studentDao.getStudentList(student, new Page(currentPage, pageSize));
        long total = studentDao.getStudentListTotal(student);

        response.setCharacterEncoding("UTF-8");
        Map<String, Object> ret = new HashMap<String, Object>();
        ret.put("total", total);
        ret.put("rows", clazzList);
        try {
            String from = request.getParameter("from");
            if ("combox".equals(from)) {
                response.getWriter().write(JSONArray.fromObject(clazzList).toString());
            } else {
                response.getWriter().write(JSONObject.fromObject(ret).toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void addStudent(HttpServletRequest request, HttpServletResponse response) {
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String sex = request.getParameter("sex");
        String mobile = request.getParameter("mobile");
        String qq = request.getParameter("qq");
        int clazzId = Integer.parseInt(request.getParameter("clazzid"));
        Student student = new Student();
        student.setClazzId(clazzId);
        student.setMobile(mobile);
        student.setName(name);
        student.setPassword(password);
        student.setQQ(qq);
        student.setSex(sex);
        student.setSn(SnGenerateUtil.generateSn(clazzId));
        StudentDao studentDao = new StudentDao();
        if (studentDao.addStudent(student)) {
            try {
                response.getWriter().write("success");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private void studentList(HttpServletRequest request,
                             HttpServletResponse response) throws IOException {
        try {
            request.getRequestDispatcher("view/studentList.jsp").forward(request, response);
        } catch (ServletException e) {
            e.printStackTrace();
        }
    }
}
